﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FasterWPF
{
    public class DockPanelSort : FasterWPF.CommonExt.ISortArgs
    {
        public DockPanelSort(CommonExt.SortDataType sortDataType)
        {
            SortDataType = sortDataType;
        }

        public DockPanelSort(CommonExt.SortDataType sortDataType, CommonExt.SortDataType sortDataType2)
        {
            SortDataType = sortDataType;
            SortDataType2 = sortDataType2;
        }

        public CommonExt.SortDataType SortDataType
        {
            get;
            set;
        }

        public CommonExt.SortDataType SortDataType2
        {
            get;
            set;
        }
    }

    public class DockArgs
    {
        public System.Windows.Controls.Dock Dock
        {
            get;
            set;
        }

        public DockArgs(System.Windows.Controls.Dock dock)
        {
            Dock = dock;
        }
    }
   
    public class DockPanelParent : FasterWPF.IParent, ICanSort
    {         
        //this is ANA?
        public object Generate<T>(object chain, T args, Action<ContentControl> addUIElementsAction)
        {
            return chain;
        }

        #region IParent

        //this is BIND
        public object Add<T>(object chain, string key, T args, Border itemToAdd)
        {
            if (args is DockArgs)
            {
                DockPanel.SetDock(itemToAdd, (args as DockArgs).Dock);
            }

            DockPanel dp = (DockPanel)chain;
            dp.Children.Add(itemToAdd);

            return chain;
        }

        public void Remove(object chain, string key, Border itemToRemove)
        {
            ((DockPanel)chain).Children.Remove(itemToRemove); //remove from DockPanel

            IComposite ic = (IComposite)(((DockPanel)chain).GetIComposite<DockPanel>());
            ic.RemoveFromChain(itemToRemove);
        }

        public Dictionary<string, object> RetrieveDictionary(object chain)
        {
            return ((DockPanel)chain).GetCompositeLookupDictionary<DockPanel>();
        }

        //this is CATA?
        public List<Border> RetrieveCollection(object chain)
        {
            List<Border> lb = new List<Border>();
            foreach (var kvp in ((DockPanel)chain).GetCompositeLookupDictionary<DockPanel>())
            {
                lb.Add((Border)kvp.Value);
            }

            return lb;
        }

        #endregion

        public void ClearCollection(object chain)
        {
            IParent currentComposite = ((DockPanel)chain).GetSetting<DockPanel, IParent>("IParent");
            List<Border> gridItems = currentComposite.RetrieveCollection(chain);

            foreach (Border borItem in gridItems)
            {
                ((DockPanel)chain).Children.Remove(borItem); //remove from Grid
            }
        }

        public void BeforeSortCollection<T>(object chain, object args, int row, int column, FasterWPF.CommonExt.SortOrder sortOrder, CommonExt.SortControlType sortControlType)
       where T : FrameworkElement
        {
            var orderedKeys = ((DockPanel)chain).GetOrderedKeyList();
            var rows = ((DockPanel)chain).GetCompositeLookupDictionary();
            Dictionary<string, object> ChildLookupDict = ((DockPanel)chain).GetChildLookupDictionary();

            IParent currentComposite = ((DockPanel)chain).GetSetting<DockPanel, IParent>("IParent");
            List<Border> gridItemsUnOrdered = currentComposite.RetrieveCollection(chain);
            List<Border> gridItems = new List<Border>();
            foreach (string s in orderedKeys)
            {
                gridItems.Add(gridItemsUnOrdered.Where(b => { return b.GetKey() == s; }).First());
            }
            //string one = gridItems[0].GetKey();
            //string two = gridItems[1].GetKey();
            //string three = gridItems[2].GetKey();

            List<int> initialOrderCol = new List<int>();
            List<int> initialOrderRow = new List<int>();
            List<string> initialOrderKey = new List<string>();

            foreach (Border borC in gridItems)
            {
                initialOrderCol.Add((int)DockPanel.GetDock(borC));
            }
            
            foreach (Border borK in gridItems)
            {
                initialOrderKey.Add(borK.GetKey());
            }

            if (gridItems != null && gridItems.Count > 0)
            {
                ClearCollection(chain);

                orderedKeys.Clear();

                IEnumerable<string> items = null;

                if (args is FasterWPF.CommonExt.ISortArgs && (((FasterWPF.CommonExt.ISortArgs)args).SortDataType) == FasterWPF.CommonExt.SortDataType.DateTime)
                {
                    items = CommonInternalExt.PerformSortOfKeysLevelOneDateTime<T>(row, column, sortOrder, ChildLookupDict, items, sortControlType);
                }
                else if (args is FasterWPF.CommonExt.ISortArgs && (((FasterWPF.CommonExt.ISortArgs)args).SortDataType) == FasterWPF.CommonExt.SortDataType.Decimal)
                {
                    items = CommonInternalExt.PerformSortOfKeysLevelOneDecimal<T>(row, column, sortOrder, ChildLookupDict, items, sortControlType);
                }
                else if (args is FasterWPF.CommonExt.ISortArgs && (((FasterWPF.CommonExt.ISortArgs)args).SortDataType) == FasterWPF.CommonExt.SortDataType.Integer)
                {
                    items = CommonInternalExt.PerformSortOfKeysLevelOneInteger<T>(row, column, sortOrder, ChildLookupDict, items, sortControlType);
                }
                else
                {
                    //Default to String
                    items = CommonInternalExt.PerformSortOfKeysLevelOne<T>(row, column, sortOrder, ChildLookupDict, items, sortControlType);
                }

                List<string> itemsToList = items.ToList();

                IterHelpers.ApplyLoop(itemsToList.Count, j =>
                {
                    String tempKey = (string)itemsToList[j];
                    Border tempBorder = gridItems[initialOrderKey.FindIndex(str => { return str == tempKey; })];
                    orderedKeys.Add(tempKey);

                    ((DockPanel)chain).Children.Add(tempBorder);

                    DockPanel.SetDock(tempBorder, (Dock)initialOrderCol[j]);
                });
            }             
        }

        public void BeforeSortCollection1Then2<T>(object chain, object args, int row, int column, int row2, int column2, FasterWPF.CommonExt.SortOrder sortOrder, FasterWPF.CommonExt.SortOrder sortOrder2, CommonExt.SortControlType sortControlType)
   where T : FrameworkElement
        {
        
        }

        public void AfterSortCollection<T>(object chain)
               where T : FrameworkElement
        { }

        public void InnerSortCollection(object chain, int row, int column, string key, ref int counter, Border itemToReAdd)
        {
            throw new NotImplementedException();
        }
        
        public void SetSelected(object chain, Border itemToSetAsSelected)
        {
        }
    }
}
